UE.plugin.register('simpleupload',
  function () {
    function c () {
      var c = b.offsetWidth || 20,
        k = b.offsetHeight || 20,
        h = document.createElement('iframe'),
        l = 'display:block;width:' + c + 'px;height:' + k + 'px;overflow:hidden;border:0;margin:0;padding:0;position:absolute;top:0;left:0;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;cursor:pointer;'
      g.on(h, 'load',
        function () {
          var b = (+new Date()).toString(36),
            d,
            m,
            r
          m = h.contentDocument || h.contentWindow.document
          r = m.body
          d = m.createElement('div')
          window.uploadcallback = function (json) {
            window._ueditor_path = json
          }
          var url = me.getOpt('serverUrl') + '&callback=uploadcallback'
          var u = '<form id="edui_form_' + b + '" target="edui_iframe_' + b + '" method="POST" enctype="multipart/form-data" action="' + url + '" style="' + l + '">'
          if (a.options.uploadFormData && typeof a.options.uploadFormData === 'object') for (var x in a.options.uploadFormData) u += '<input type="hidden" name="' + x + '" value="' + a.options.uploadFormData[x] + '">'
          u = u + '<input id="edui_input_' + b + '" type="file" accept="image/gif,image/jpg,image/jpeg,image/png"  name="' + a.options.imageFieldName + '" style="' + l + '">'
          d.innerHTML = u + '</form><iframe id="edui_iframe_' + b + '" name="edui_iframe_' + b + '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>'
          d.className = 'edui-' + a.options.theme
          d.id = a.ui.id + '_iframeupload'
          r.style.cssText = l
          r.style.width = c + 'px'
          r.style.height = k + 'px'
          r.appendChild(d)
          r.parentNode && (r.parentNode.style.width = c + 'px', r.parentNode.style.height = c + 'px')
          var y = m.getElementById('edui_form_' + b),
            v = m.getElementById('edui_input_' + b),
            q = m.getElementById('edui_iframe_' + b)
          g.on(v, 'change',
            function () {
              function b () {
                try {
                  var f, e, h, k
                  var e = window._ueditor_path
                  var f = json.url

                  e.state == 'SUCCESS' && e.url ? (h = a.document.getElementById(d), g.removeClasses(h, 'loadingclass'), h.setAttribute('src', f), h.setAttribute('_src', f), h.setAttribute('alt', e.original || ''), e.aid && (h.setAttribute('aid', 'attachimg_' + e.aid), h.setAttribute('id', 'aimg_' + e.aid)), a.options.uploadCallback && typeof a.options.uploadCallback === 'function' && a.options.uploadCallback(e), h.removeAttribute('id')) : c && c(e.state)
                } catch (ca) {
                  c && c(a.getLang('simpleupload.loadError'))
                }
                y.reset()
                g.un(q, 'load', b)
              }
              function c (b) {
                if (d) {
                  var c = a.document.getElementById(d)
                  c && g.remove(c)
                  a.fireEvent('showmessage', {
                    id: d,
                    content: b,
                    type: 'error',
                    timeout: 4E3
                  })
                }
              }
              if (v.value) {
                var d = 'loading_' + (+new Date()).toString(36),
                  f = p.serializeParam(a.queryCommandValue('serverparam')) || '',
                  e = a.getActionUrl(a.getOpt('imageActionName')),
                  h = a.getOpt('imageAllowFiles')
                a.focus()
                a.execCommand('inserthtml', '<img class="loadingclass" id="' + d + '" src="' + a.options.themePath + a.options.theme + '/images/spacer.gif">')
                if (a.getOpt('imageActionName')) {
                  var k = v.value,
                    k = k ? k.substr(k.lastIndexOf('.')) : ''; !k || h && (h.join('') + '.').indexOf(k.toLowerCase() + '.') == -1 ? c(a.getLang('simpleupload.exceedTypeError')) : (h = v.files[0]) && h.size > a.getOpt('imageMaxSize') ? (v.value = '', c(a.getLang('simpleupload.exceedSizeError'))) : (g.on(q, 'load', b), y.action = p.formatUrl(e + (e.indexOf('?') == -1 ? '?domain=1&' : '&domain=1&') + f), y.submit())
                } else errorHandler(a.getLang('autoupload.errorLoadConfig'))
              }
            })
          var t
          a.addListener('selectionchange',
            function () {
              clearTimeout(t)
              t = setTimeout(function () {
                a.queryCommandState('simpleupload') == -1 ? v.disabled = 'disabled' : v.disabled = !1
              },
              400)
            })
          e = !0
        })
      h.style.cssText = l
      b.appendChild(h)
    }
    var a = this,
      e = !1,
      b
    return {
      bindEvents: {
        ready: function () {
          p.cssRule('loading', ".loadingclass{display:inline-block;cursor:default;background: url('" + this.options.themePath + this.options.theme + "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n.loaderrorclass{display:inline-block;cursor:default;background: url('" + this.options.themePath + this.options.theme + "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}", this.document)
        },
        simpleuploadbtnready: function (d, e) {
          b = e
          a.afterConfigReady(c)
        }
      },
      outputRule: function (a) {
        p.each(a.getNodesByTagName('img'),
          function (a) {
            / \b(loaderrorclass) | (bloaderrorclass)\b /.test(a.getAttr('class')) && a.parentNode.removeChild(a)
          })
      },
      commands: {
        simpleupload: {
          queryCommandState: function () {
            return e ? 0 : -1
          }
        }
      }
    }
  })
